// 当前对局数据
let currentGameRecords;
const STORAGE_KEYS = require('../configs/STORAGE_KEYS')
const { getInsertIndex, getLeftStickNumOfRecords } = require('../business/records')

const connectors = [];

function invokeConnectors() {
  connectors.forEach(fn => {
    if (typeof fn === 'function') {
      fn()
    }
  })
}

module.exports = {
  addConnector(fn) {
    connectors.push(fn);
  },
  /**
   * 获取当前对局数据
   */
  getCurrentGameRecords() {
    return currentGameRecords ? currentGameRecords : currentGameRecords = (wx.getStorageSync(STORAGE_KEYS.CURRENT_GAME_RECORDS) || [])
  },
  /**
   * 存储当前对局数据
   * @param {object} record 当前对局数据
   */
  storeCurrentGameRecord(record) {
    const currentGameRecords = this.getCurrentGameRecords()
    const inserIndex = getInsertIndex(currentGameRecords, record)
    record.key = String(Date.now())
    currentGameRecords.splice(inserIndex, 0, record);
    this.storeRecords(currentGameRecords)
  },
  removeCurrentRecord(record) {
    const currentGameRecords = this.getCurrentGameRecords()
    const index = currentGameRecords.findIndex(item => item.key === record.key);
    if (index > -1) {
      currentGameRecords.splice(index, 1)
    }
    this.storeRecords(currentGameRecords)
  },
  storeRecords(currentGameRecords) {
    wx.setStorageSync(STORAGE_KEYS.CURRENT_GAME_RECORDS, currentGameRecords)
    invokeConnectors();
  },
  getCurrentRecordByKey(key) {
    const currentGameRecords = this.getCurrentGameRecords()
    return currentGameRecords.find(record => record.key === key);
  },
  putCurrentGameRecord(formData, record) {
    formData.key = String(Date.now());
    const currentGameRecords = this.getCurrentGameRecords()
    const index = currentGameRecords.findIndex(item => item.key === record.key);
    currentGameRecords.splice(index, 1, formData)
    this.storeRecords(currentGameRecords)
  },
  /**
   * 获取当前对局剩余的立直棒数量
   */
  getCurrentLeftStickNum() {
    const currentGameRecords = this.getCurrentGameRecords()
    return getLeftStickNumOfRecords(currentGameRecords)
  },
  storeCurrentToHistory(remark) {
    const gameNum = this.generateGameNum();
    const historyGameNums = wx.getStorageSync(STORAGE_KEYS.HISTORY_GAME_NUMS) || [];
    if (!historyGameNums.includes(gameNum)) {
      historyGameNums.push(gameNum);
    }
    const currentRecords = wx.getStorageSync(STORAGE_KEYS.CURRENT_GAME_RECORDS);
    wx.setStorageSync(gameNum, currentRecords);
    wx.setStorageSync(STORAGE_KEYS.HISTORY_GAME_NUMS, historyGameNums);
    if (remark) {
      const gameRemarks = wx.getStorageSync(STORAGE_KEYS.GAME_REMARKS) || {};
      gameRemarks[gameNum] = remark;
      wx.setStorageSync(STORAGE_KEYS.GAME_REMARKS, gameRemarks);
    }
  },
  generateGameNum() {
    const currentPlayerNums = wx.getStorageSync(STORAGE_KEYS.CURRENT_PLAYER_NUMS);
    const currentStartTime = wx.getStorageSync(STORAGE_KEYS.CURRENT_START_TIME);
    return `GAME_${currentStartTime}_${currentPlayerNums.join('-')}`;
  },
  clearCurrent(shouldKeepPlayer = false) {
    wx.removeStorageSync(STORAGE_KEYS.CURRENT_GAME_RECORDS)
    currentGameRecords = []
    if (!shouldKeepPlayer) {
      wx.removeStorageSync(STORAGE_KEYS.CURRENT_PLAYER_NUMS)
    }
    wx.removeStorageSync(STORAGE_KEYS.CURRENT_START_TIME)
    wx.removeStorageSync(STORAGE_KEYS.CURRENT_GAME_NUM)
  },
  clearHistoryGame(gameNum) {
    let historyGameNums = wx.getStorageSync(STORAGE_KEYS.HISTORY_GAME_NUMS) || [];
    if (historyGameNums.includes(gameNum)) {
      wx.setStorageSync(STORAGE_KEYS.HISTORY_GAME_NUMS, historyGameNums.filter(num => num !== gameNum))
    }
    wx.removeStorageSync(gameNum)
  },
  // 编辑历史牌局
  continueGameNum(gameNum) {
    const records = wx.getStorageSync(gameNum);
    let [, startTime, playerStr] = gameNum.split('_');
    startTime = Number(startTime);
    const playerNums = playerStr.split('-').map(Number);
    if (records) {
      wx.setStorageSync(STORAGE_KEYS.CURRENT_START_TIME, startTime)
      wx.setStorageSync(STORAGE_KEYS.CURRENT_GAME_NUM, gameNum)
      wx.setStorageSync(STORAGE_KEYS.CURRENT_PLAYER_NUMS, playerNums)
      wx.setStorageSync(STORAGE_KEYS.CURRENT_GAME_RECORDS, records)
    }
  }
}